package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataGetDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.FormDataGetVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.detailtab.dto.MyTeamGetDTO;
import com.xbongbong.pro.detailtab.vo.MyTeamGetVO;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.ChangeWorkflowDTO;
import com.xbongbong.pro.workorder.pojo.dto.ChangeWorkflowItemDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderCcGetDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderDataDetailDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderMyPendingDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderProgressDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderCcGetVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderChangeFlowItemVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderDataDetailVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderDataEsListVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderMyPendingVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderProcessVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.BusinessProductService;
import com.xbongbong.saas.service.SaasDetailTabService;
import com.xbongbong.saas.service.WorkOrderCcService;
import com.xbongbong.saas.service.WorkOrderFormDataService;
import com.xbongbong.saas.service.WorkOrderService;
import com.xbongbong.workorder.service.WorkOrderBasicService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/4/16 1:56 PM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workOrder/center")
public class WorkOrderCenterController {

    @Resource
    private SaasDetailTabService saasDetailTabService;
    @Resource
    private WorkOrderFormDataService workOrderFormDataService;
    @Resource
    private BusinessProductService businessProductService;
    @Resource
    private WorkOrderService workOrderService;
    @Resource
    private WorkOrderCcService workOrderCcService;
    @Resource
    private WorkOrderBasicService workOrderBasicService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * Description: 获取数据详情tab页面
     * @param workOrderDataDetailDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/detail",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid WorkOrderDataDetailDTO workOrderDataDetailDTO, BindingResult br) {
        XbbResponse<WorkOrderDataDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderDataDetailVO workOrderDataDetailVO = workOrderFormDataService.getDetail(workOrderDataDetailDTO);
                response = new XbbResponse<>(workOrderDataDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 负责团队
     * @param myTeamGetDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/18 2:00 PM
     * @since v1.0
     */
    @RequestMapping(value = "/myTeam/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMyTeam(@RequestBody @Valid MyTeamGetDTO myTeamGetDTO, BindingResult br) {
        XbbResponse<MyTeamGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MyTeamGetVO myTeamGetVO = saasDetailTabService.getMyTeam(myTeamGetDTO);
                response = new XbbResponse<>(myTeamGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 实施备件
     * @param detailTabDataGetDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/23 9:57 AM
     * @since v1.0
     */
//    @RequestMapping(value = "/productList/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String getProductList(@RequestBody @Valid DetailTabDataGetDTO detailTabDataGetDTO, BindingResult br) {
//        XbbResponse<BusinessProductListVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                BusinessProductListVO businessProductListVO = businessProductService.getBusinessProductList(detailTabDataGetDTO);
//                response = new XbbResponse<>(businessProductListVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
//    }
    /**
     * Description: 工单内容详情
     * @param formDataGetDTO
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/4/16 2:22 PM
     * @since v1.0
     */
    @RequestMapping(value = "/get",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid FormDataGetDTO formDataGetDTO, BindingResult br) {
        XbbResponse<FormDataGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataGetDTO.getSaasMark(), formDataGetDTO.getBusinessType(), formDataGetDTO.getMenuId(), formDataGetDTO.getDistributorMark());
                FormDataGetVO formDataGetVO = workOrderFormDataService.get(formDataGetDTO);
                response = new XbbResponse<>(formDataGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 工单列表
     * @param formDataListDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<WorkOrderDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                WorkOrderDataEsListVO workOrderDataEsListVO = workOrderFormDataService.normalList(formDataListDTO);
                response = new XbbResponse<>(workOrderDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 抢单工单列表
     * @param formDataListDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/robList",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String robList(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) {
        XbbResponse<WorkOrderDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                WorkOrderDataEsListVO workOrderDataEsListVO = workOrderFormDataService.robList(formDataListDTO);
                response = new XbbResponse<>(workOrderDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * Description: 工单进展
     * @param workOrderProgressDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/progress",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String progress(@RequestBody @Valid WorkOrderProgressDTO workOrderProgressDTO, BindingResult br) {
        XbbResponse<WorkOrderProcessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderProcessVO workOrderProcessVO = workOrderFormDataService.progress(workOrderProgressDTO);
                response = new XbbResponse<>(workOrderProcessVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * Description: 工单流转弹窗
     * @param changeWorkflowItemDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/getChangeFlowItem",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getChangeFlowItem(@RequestBody @Valid ChangeWorkflowItemDTO changeWorkflowItemDTO, BindingResult br) {
        XbbResponse<WorkOrderChangeFlowItemVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderChangeFlowItemVO workOrderChangeFlowItemVO = workOrderService.getChangeFlowItem(changeWorkflowItemDTO);
                response = new XbbResponse<>(workOrderChangeFlowItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 工单进展
     * @param changeWorkflowDTO
     * @return java.lang.String
     * @author haibin.zhang
     * @date 2019/4/16 2:02 PM
     * @since v1.0
     */
    @RequestMapping(value = "/changeWorkflow",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeWorkflow(@RequestBody @Valid ChangeWorkflowDTO changeWorkflowDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(changeWorkflowDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(changeWorkflowDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }

                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), null, DistributorMarkEnum.OTHER.getCode());
                BaseVO baseVO = new BaseVO();
                workOrderBasicService.changeWorkflow(changeWorkflowDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 获取工单抄送人员
     * @param workOrderCcGetDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/5/1 9:32
     * @since v1.0
     */
    @RequestMapping(value = "/getCc",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCc(@RequestBody @Valid WorkOrderCcGetDTO workOrderCcGetDTO, BindingResult br) {
        XbbResponse<WorkOrderCcGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderCcGetVO workOrderCcGetVO = workOrderCcService.getCc(workOrderCcGetDTO);
                response = new XbbResponse<>(workOrderCcGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 首页获取待我处理（待接收和进行中）的工单列表卡片
     * @param workOrderMyPendingDTO
     * @param br
     * @return java.lang.String
     * @author 刘阳
     * @date 2019/5/28 3:58 PM
     * @since v1.0
     */
    @RequestMapping(value = "/getMyPendingWorkOrderList",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMyPendingWorkOrderList(@RequestBody @Valid WorkOrderMyPendingDTO workOrderMyPendingDTO, BindingResult br) {
        XbbResponse<WorkOrderMyPendingVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), null, workOrderMyPendingDTO.getDistributorMark());
                WorkOrderMyPendingVO workOrderMyPendingVO = workOrderFormDataService.getMyPendingWorkOrderList(workOrderMyPendingDTO);
                response = new XbbResponse<>(workOrderMyPendingVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取工单进展和工单动态相关的数据
     * @param formDataGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getPrintRelationData",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPrintRelationData(@RequestBody @Valid FormDataGetDTO formDataGetDTO, BindingResult br) {
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                JSONObject printRelationData = workOrderFormDataService.getPrintRelationData(formDataGetDTO);
                response = new XbbResponse<>(printRelationData);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
